'use strict';

angular.module('CBO').controller('ShiftController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseFunction, $location, Authentication) {
        //$scope.authentication = Authentication;
        var oldUniqueID = '';
        
        $scope.btnSave = function(){
            if($scope.Module.Shift.FocusedRecord.Code == undefined || 
                $scope.Module.Shift.FocusedRecord.Code.trim() == ""){
                BaseFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Shift.FocusedRecord.Name == undefined || 
                $scope.Module.Shift.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Shift.FocusedRecord.StrStartTime == undefined || 
                $scope.Module.Shift.FocusedRecord.StrStartTime.trim() == ""){
                BaseFunction.alert("开始时间不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Shift.FocusedRecord.StrEndTime == undefined || 
                $scope.Module.Shift.FocusedRecord.StrEndTime.trim() == ""){
                BaseFunction.alert("结束时间不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Shift.FocusedRecord.StrScanStartTime == undefined || 
                $scope.Module.Shift.FocusedRecord.StrScanStartTime.trim() == ""){
                BaseFunction.alert("刷卡开始时间不能为空！", "保存错误");
                return;
            }

            var scanStartTimes = $scope.Module.Shift.FocusedRecord.StrScanStartTime.split(':');
            var startTimes = $scope.Module.Shift.FocusedRecord.StrStartTime.split(':');
            var endTimes = $scope.Module.Shift.FocusedRecord.StrEndTime.split(':');
            var reg = new RegExp(/^[0-9]{1,2}:[0-9]{1,2}$/);
            if(!reg.test($scope.Module.Shift.FocusedRecord.StrStartTime) || 
                parseInt(startTimes[0]) >= 24 || parseInt(startTimes[1]) >= 60){
                BaseFunction.alert("开始时间必须严格按照24小时时间格式来编写.\n例如：08:00或者8:0", "保存错误");
                return;
            }
            if(!reg.test($scope.Module.Shift.FocusedRecord.StrEndTime) || 
                parseInt(endTimes[0]) >= 24 || parseInt(endTimes[1]) >= 60){
                BaseFunction.alert("结束时间必须严格按照24小时时间格式来编写.\n例如：18:00或者18:0", "保存错误");
                return;
            }
            if(!reg.test($scope.Module.Shift.FocusedRecord.StrScanStartTime) || 
                parseInt(scanStartTimes[0]) >= 24 || parseInt(scanStartTimes[1]) >= 60){
                BaseFunction.alert("刷卡开始时间必须严格按照24小时时间格式来编写.\n例如：18:00或者18:0", "保存错误");
                return;
            }

            var startDateTime = new Date(2000, 0, 1, parseInt(startTimes[0]), parseInt(startTimes[1]), 0);
            var endDateTime;
            if(parseInt(startTimes[0]) * 60 + parseInt(startTimes[1]) > parseInt(endTimes[0]) * 60 + parseInt(endTimes[1])){
                endDateTime = new Date(2000, 0, 2, parseInt(endTimes[0]), parseInt(endTimes[1]), 0);
            }
            else{
                endDateTime = new Date(2000, 0, 1, parseInt(endTimes[0]), parseInt(endTimes[1]), 0);
            }

            if(startTimes[0].length == 1){
                startTimes[0] = "0" + startTimes[0];
            }
            if(startTimes[1].length == 1){
                startTimes[1] = "0" + startTimes[1];
            }

            if(endTimes[0].length == 1){
                endTimes[0] = "0" + endTimes[0];
            }
            if(endTimes[1].length == 1){
                endTimes[1] = "0" + endTimes[1];
            }

            var scanStartTime = new Date(2000, 0, 1, parseInt(startTimes[0]), parseInt(startTimes[1]), 0);
            if(scanStartTimes[0].length == 1){
                scanStartTimes[0] = "0" + scanStartTimes[0];
            }
            if(scanStartTimes[1].length == 1){
                scanStartTimes[1] = "0" + scanStartTimes[1];
            }


            $scope.Module.Shift.FocusedRecord.StartTime = startDateTime;
            $scope.Module.Shift.FocusedRecord.StrStartTime = startTimes[0] + ":" + startTimes[1];
            $scope.Module.Shift.FocusedRecord.EndTime = endDateTime;
            $scope.Module.Shift.FocusedRecord.StrEndTime = endTimes[0] + ":" + endTimes[1];
            $scope.Module.Shift.FocusedRecord.ScanStartTime = scanStartTime;
            $scope.Module.Shift.FocusedRecord.StrScanStartTime = scanStartTimes[0] + ":" + scanStartTimes[1];
            $scope.Module.Shift.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.Shift.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.Shift.add();
        };
        $scope.btnDelete = function(){
            $scope.Module.Shift.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/Shift/List');
        };

        //初始化
        BaseModule.initModule($scope, 'Shift');
        $scope.Module.Shift.AfterOnAdd = function(record){
            record.IsEffective = true;
            record.StrScanStartTime = "07:00";
            record.StrStartTime = "08:00";
            record.StrEndTime = "12:00";
        };

        if($stateParams.id) {
            $scope.Module.Shift.findById($stateParams.id, function(record){
                if(record != undefined && record != null){
                    $scope.Module.Shift.FocusedRecord = record;
                }
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);
